package com.bvbot.core.session;

import static com.bvbot.core.Logger.log;

import java.math.BigDecimal;

public class BalanceResponse extends BvResponse {

	private static final String AUXZU = "AUXZU";
	private static final String AGXLN = "AGXLN";

	private int goldGrams = -1;
	private int silverGrams = -1;
	private String dollarsAsString = null;
	private String eurosAsString = null;

	public BalanceResponse(String content) {
		super(content);
	}

	public int getSilverGrams() throws BvSessionException {
		if (silverGrams == -1) {
			try {
				int indexAgxLn = getContent().indexOf(AGXLN);
				if (indexAgxLn != -1) { // silver owned
					int indexTagEndAfterAgxLn = getContent().indexOf(">",
							indexAgxLn);
					String subString = getContent().substring(indexAgxLn,
							indexTagEndAfterAgxLn);
					String availableString = extractAttributValue("available",
							subString);
					BigDecimal sValueBd = getValueMultipliedBy1000(availableString);
					if (sValueBd != null) {
						silverGrams = sValueBd.intValueExact();
					}
				} else {
					silverGrams = 0;
				}
			} catch (Exception e) {
				String message = "Error while computing available silver grams in London: "
						+ e.getMessage();
				log(message, e);
				throw new BvSessionException(message, e);
			}
		}
		return silverGrams;
	}

	public int getGoldGrams() throws BvSessionException {
		if (goldGrams == -1) {
			try {
				int indexAuxZu = getContent().indexOf(AUXZU);
				if (indexAuxZu != -1) { // gold owned
					int indexTagEndAfterAuxZu = getContent().indexOf(">",
							indexAuxZu);
					String subString = getContent().substring(indexAuxZu,
							indexTagEndAfterAuxZu);
					String availableString = extractAttributValue("available",
							subString);
					BigDecimal gValueBd = getValueMultipliedBy1000(availableString);
					if (gValueBd != null) {
						goldGrams = gValueBd.intValueExact();
					}
				} else {
					goldGrams = 0;
				}
			} catch (Exception e) {
				String message = "Error while computing available gold grams in Zurich : "
						+ e.getMessage();
				log(message, e);
				throw new BvSessionException(message, e);
			}
		}
		return goldGrams;
	}

	public String getDollars() throws BvSessionException {
		if (dollarsAsString == null) {
			try {
				int indexAuxZu = getContent().indexOf("securityId=\"USD\"");
				int indexTagEndAfterAuxZu = getContent().indexOf(">",
						indexAuxZu);
				if (indexAuxZu > 0) {
					String subString = getContent().substring(indexAuxZu,
							indexTagEndAfterAuxZu);
					int indexAvailable = subString.indexOf("available");
					int lastDoubleQuote = subString.indexOf("\"",
							indexAvailable + 11);
					String availableString = subString.substring(
							indexAvailable + 11, lastDoubleQuote);
					dollarsAsString = availableString;
				} else {
					dollarsAsString = "0.00";
				}
			} catch (Exception e) {
				String message = "Error while computing available dollars : "
						+ e.getMessage();
				log(message, e);
				throw new BvSessionException(message, e);
			}
		}
		return dollarsAsString;
	}

	public BigDecimal getDollarsAsBd() throws BvSessionException {
		String dollString = getDollars();
		return new BigDecimal(dollString);
	}

	public String getEuros() throws BvSessionException {
		if (eurosAsString == null) {
			try {
				int indexAuxZu = getContent().indexOf("securityId=\"EUR\"");
				int indexTagEndAfterAuxZu = getContent().indexOf(">",
						indexAuxZu);
				if (indexAuxZu > 0) {
					String subString = getContent().substring(indexAuxZu,
							indexTagEndAfterAuxZu);
					int indexAvailable = subString.indexOf("available");
					int lastDoubleQuote = subString.indexOf("\"",
							indexAvailable + 11);
					String availableString = subString.substring(
							indexAvailable + 11, lastDoubleQuote);
					eurosAsString = availableString;
				} else {
					eurosAsString = "0.00";
				}
			} catch (Exception e) {
				String message = "Error while computing available euros : "
						+ e.getMessage();
				log(message, e);
				throw new BvSessionException(message, e);
			}
		}
		return eurosAsString;
	}

}
